<template>
  <div class="ele-body">
    <el-card shadow="never">
      <!-- 搜索表单 -->
      <el-form :model="where" class="ele-form-search" label-width="77px" @keyup.enter.native="reload"
               @submit.native.prevent>
        <el-row :gutter="15">
          <el-col :lg="6" :md="12">
            <el-form-item label="搜索查询:">
              <el-input v-model="where.keyword" clearable placeholder="输入关键词"/>
            </el-form-item>
          </el-col>
          <el-col :lg="6" :md="12">
            <div class="ele-form-actions">
              <el-button v-if="permission.includes('sys:bom:index')" class="ele-btn-icon" icon="el-icon-search"
                         type="primary" @click="reload">
                查询
              </el-button>
              <el-button @click="reset">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
      <!-- 数据表格 -->
      <ele-pro-table ref="table" :columns="columns" :datasource="url" :selection.sync="selection" :where="where"
                     height="calc(100vh - 312px)">
        <!-- 表头工具栏 -->
        <template slot="toolbar">
          <el-button v-if="permission.includes('sys:accessories:add')" class="ele-btn-icon" icon="el-icon-plus"
                     size="small"
                     type="primary" @click="onOpenEdit(null)">
            新建备件
          </el-button>
        </template>

        <template #c_number="{row}">
          <el-link type="primary" @click="onGoToInfo(row)" :underline="false">{{ row.c_number }}</el-link>
        </template>

        <template #status="{row}">
          <el-switch
            :disabled="!permission.includes('sys:accessories:edit')"
            :value="row.status"
            :active-value="1"
            :inactive-value="0"
            @change="(state)=>onChangeState(state,row)"
          >
          </el-switch>
        </template>

        <template #action="{row}">
          <el-link v-show="permission.includes('sys:accessories:edit')" :underline="false" icon="el-icon-edit"
                   type="primary"
                   @click="onOpenEdit(row,'put')">
            修改
          </el-link>
          <el-popconfirm class="ele-action" title="确定要删除此备件吗？" @confirm="remove(row.id)">
            <el-link v-show="permission.includes('sys:accessories:delete')" slot="reference" :underline="false"
                     icon="el-icon-delete" type="danger">
              删除
            </el-link>
          </el-popconfirm>
        </template>
      </ele-pro-table>

    </el-card>
    <AddDialog @reload="reload" ref="addDialog" :current="current"/>
  </div>
</template>

<script>
import {mapGetters} from "vuex";
import AddDialog from "@/views/bom/accessories/components/add-dialog.vue";


export default {
  name: 'SystemLevel',
  components: {
    AddDialog,

  },
  computed: {
    ...mapGetters(["permission"]),
  },
  data() {
    return {
      current: {},
      // 表格数据接口
      url: '/part_categories',
      // 表格列配置
      columns: [
        // {
        //   // columnKey: 'selection',
        //   type: 'selection',
        //   width: 45,
        //   align: 'center',
        //   fixed: "left"
        // },
        {
          slot: 'c_number',
          label: '编号',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'name',
          label: '名称',
          width: 150,
          align: 'center',
          showOverflowTooltip: true,
        },
        {
          prop: 'type',
          label: '类别',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'model',
          label: '规格型号',
          showOverflowTooltip: true,
          minWidth: 100,
          align: 'center',
        },
        {
          prop: 'uom',
          label: '单位',
          showOverflowTooltip: true,
          minWidth: 150,
          align: 'center',
        },
        {
          prop: 'sale_price',
          label: '售价',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'out_price',
          label: '成本价',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          slot: 'status',
          label: '状态',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          prop: 'create_time',
          label: '入库时间',
          showOverflowTooltip: true,
          minWidth: 160,
          align: 'center'
        },
        {
          columnKey: 'action',
          label: '操作',
          width: 180,
          align: 'center',
          resizable: false,
          slot: 'action',
          fixed: "right"
        }
      ],
      // 表格搜索条件
      where: {},
      // 表格选中数据
      selection: [],
    };
  },
  watch:{
    $route() {
      if (this.$router.history.current.path === '/bom/accessories') {
        this.reload()
      }
    }
  },
  methods: {
    /* 刷新表格 */
    reload() {
      this.$refs.table.reload({where: this.where});
    },
    /* 重置搜索 */
    reset() {
      this.where = {};
      this.reload();
    },
    /*删除配件*/
    remove(id) {
      this.$http.delete(`/part_categories/${id}`).then(res => {
          res.data.success ? this.$message.success(res.data.msg) : this.$message.error(res.data.msg)
          this.reload()
        }
      )
    },
    /*编辑配件*/
    onOpenEdit(row, httpMethod) {
      this.current = row;
      this.$refs.addDialog.onActive(httpMethod)
    },
    onChangeState(state, row) {
      let url = `/part_categories/${row.id}/`
      url += state == 1 ? `active` : `inactive`
      this.$http.post(url).then(res => {
        res.data.success ? this.$message.success(res.data.msg) : this.$message.error(res.data.msg)
        this.reload()
      })
    },
    onGoToInfo(row) {
      this.$router.push({
        path: '/bom/accessories/info',
        query: {id: row.id}
      })
    },
  }
}
</script>

<style scoped lang="scss">

.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;

  span {
    font-size: 18px;
    font-weight: bold;
  }

  > div {
    display: flex;

    > button {
      margin-left: 10px;
    }
  }
}
</style>
